<?php

/**

 * [WeEngine System] Copyright (c) 2014 WE7.CC

 * WeEngine is NOT a free software, it under the license terms, visited http://www.qincewang.com/ for more details.

 */

defined('IN_IA') or exit('Access Denied');

$dos = array('sign_display', 'sign', 'sign_record', 'notice', 'sign_strategy', 'share', 'mycard', 'add_recharge', 'addnums', 'recharge_record', 'receive_card', 'personal_info', 'consume');

$do = in_array($do, $dos) ? $do : 'mycard';

load()->model('user');

load()->model('card');

load()->func('tpl');

activity_coupon_type_init();

$notice_count = card_notice_stat();

$setting = pdo_get('mc_card', array('uniacid' => $_W['uniacid']));

if($do == 'sign_display') {

	$title = '签到-会员卡';

	$credit_set = card_credit_setting();

	$sign_set = $credit_set['sign'];

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_card_sign_record') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid']));

	$current_month_days = date('t', TIMESTAMP);

	$sign_rules = array(

		$sign_set['first_group_day'] => $sign_set['first_group_num'],

		$sign_set['second_group_day'] => $sign_set['second_group_num'],

		$sign_set['third_group_day'] => $sign_set['third_group_num'],

		$current_month_days => $sign_set['full_sign_num'],

	);

	if (!empty($sign_rules[$total])) {

		$today_sign_credit = $sign_rules[$total];

	} else {

		$today_sign_credit = $sign_set['everydaynum'];

	}

	if (!empty($sign_rules[$total + 1])) {

		$tomorrow_sign_credit = $sign_rules[$total + 1];

	} else {

		$tomorrow_sign_credit = $sign_set['everydaynum'];

	}

	$data = array(

		'uniacid' => $_W['uniacid'],

		'uid' => $_W['member']['uid'],

		'credit' => $today_sign_credit,

		'is_grant' => 0,

		'addtime' => TIMESTAMP,

	);

	$today_signed = pdo_get('mc_card_sign_record', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid'], 'addtime >' => strtotime(date('Y-m-d'))), 'id');

	if(empty($today_signed)) {

		$status = pdo_insert('mc_card_sign_record', $data);

		if (!empty($status) && $today_sign_credit > 0) {

			$log = "用户签到赠送【{$today_sign_credit}】积分";

			mc_credit_update($_W['member']['uid'], 'credit1', $today_sign_credit, array(0, $log, 'sign'));

			mc_notice_credit1($_W['openid'], $_W['member']['uid'], $today_sign_credit, $log);

		}

		$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_card_sign_record') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid']));

	}

}



if($do == 'sign_record') {

	$title = '签到记录-会员卡';

	$psize = 20;

	$pindex = max(1, intval($_GPC['page']));

	$period = intval($_GPC['period']);

	if ($period == '1') {

		$starttime = date('Ym01',strtotime(0));

		$endtime = date('Ymd His', time());

	} elseif($period == '0') {

		$starttime = date('Ym01', strtotime(1*$period . "month"));

		$endtime = date('Ymd', strtotime("$starttime + 1 month - 1 day"));

	} else {

		$starttime = date('Ym01', strtotime(1*$period . "month"));

		$endtime = date('Ymd', strtotime("$starttime + 1 month - 1 day"));

	}

	$where = '';

	$params = array();

	$where = ' WHERE `uniacid` = :uniacid AND `uid` = :uid AND `addtime` >= :starttime AND `addtime` < :endtime';

	$params[':uniacid'] = $_W['uniacid'];

	$params[':uid'] = $_W['member']['uid'];

	$params[':starttime'] = strtotime($starttime);

	$params[':endtime'] = strtotime($endtime);

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_card_sign_record') . $where, $params);

	$data = pdo_fetchall('SELECT * FROM ' . tablename('mc_card_sign_record') . $where . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ", {$psize}", $params);

	$pagenums = ceil($total / $psize);

	foreach ($data as &$value) {

		$value['addtime'] = date('Y.m.d', $value['addtime']);

	}

	if($_W['isajax'] && $_W['ispost']) {

		if (!empty($data)){

			message(error(0, $data), '', 'ajax');

		} else {

			message(error(1, 'error'), '', 'ajax');

		}

	}

}



if($do == 'sign_strategy') {

	$set = card_credit_setting();

	$content = $set['content'];

}



if($do == 'notice') {

	$title = '系统消息-会员卡';

	if($_W['isajax']) {

		$id = intval($_GPC['id']);

		if($id > 0) {

			pdo_update('mc_card_notices_unread', array('is_new' => 0), array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid'], 'notice_id' => $id));

			$total = card_notice_stat();

			exit($total);

		}

	}

	$psize = 20;

	$pindex = max(1, intval($_GPC['page']));

	$type = intval($_GPC['type']) ? intval($_GPC['type']) : 1;

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_card_notices_unread') . ' WHERE uniacid = :uniacid AND uid = :uid AND type = :type', array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid'], ':type' => $type));

	$data = pdo_fetchall('SELECT a.*,b.* FROM ' . tablename('mc_card_notices_unread') . ' AS a LEFT JOIN ' . tablename('mc_card_notices') . ' AS b ON a.notice_id = b.id WHERE a.uniacid = b.uniacid AND a.uniacid = :uniacid AND a.uid = :uid AND a.type = :type ORDER BY a.notice_id DESC LIMIT ' . ($pindex - 1) * $psize . ", {$psize}", array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid'], ':type' => $type));

	$pager = pagination($total, $pindex, $psize);

}





if ($do == 'receive_card') {

	$mcard = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']), array('id'));

	if(!empty($mcard)) {

		header('Location:' . url('mc/card/mycard'));

		exit;

	}

	

	$sql = 'SELECT * FROM ' . tablename('mc_card') . " WHERE `uniacid` = :uniacid AND `status` = '1'";

	$setting = pdo_fetch($sql, array(':uniacid' => $_W['uniacid']));

	if (!empty($setting)) {

		$setting['color'] = iunserializer($setting['color']);

		$setting['background'] = iunserializer($setting['background']);

		$setting['fields'] = iunserializer($setting['fields']);

		$setting['grant'] = iunserializer($setting['grant']);

		if (!empty($setting['grant']['coupon']) && is_array($setting['grant']['coupon'])) {

			foreach ($setting['grant']['coupon'] as $grant_coupon) {

				$coupon_title .= "{$grant_coupon['couponTitle']}|";

			}

		}

	} else {

		message('公众号尚未开启会员卡功能', url('mc'), 'error');

	}



	if(!empty($setting['fields'])) {

		$fields = array('email');

		foreach($setting['fields'] as $li) {

			if($li['bind'] == 'birth') {

				$fields[] = 'birthyear';

				$fields[] = 'birthmonth';

				$fields[] = 'birthday';

			} elseif($li['bind'] == 'reside') {

				$fields[] = 'resideprovince';

				$fields[] = 'residecity';

				$fields[] = 'residedist';

			} else {

				$fields[] = $li['bind'];

			}

		}

		$member_info = mc_fetch($_W['member']['uid'], $fields);

		$reregister = 0;

		if(strlen($member_info['email']) == 39 && strexists($member_info['email'], '@we7.cc')) {

			$member_info['email'] = '';

			$reregister = 1;

		}

	}

	if ($_W['isajax'] && $_W['ispost']) {

		$data = array();

		$realname = trim($_GPC['realname']);

		if(empty($realname)) {

			message('请输入姓名', referer(), 'info');

		}

		$data['realname'] = $realname;

		$mobile = trim($_GPC['mobile']);

		if (!preg_match(REGULAR_MOBILE, $mobile)) {

			message('手机号有误', referer(), 'info');

		}

		$data['mobile'] = $mobile;

		if (!empty($setting['fields'])) {

			foreach ($setting['fields'] as $row) {

				if (!empty($row['require']) && ($row['bind'] == 'birth' || $row['bind'] == 'birthyear')) {

					if (empty($_GPC['birth']['year']) || empty($_GPC['birth']['month']) || empty($_GPC['birth']['day'])) {

						message('请输入出生日期', referer(), 'info');

					}

					$row['bind'] = 'birth';

				} elseif (!empty($row['require']) && $row['bind'] == 'resideprovince') {

					if (empty($_GPC['reside']['province']) || empty($_GPC['reside']['city']) || empty($_GPC['reside']['district'])) {

						message('请输入居住地', referer(), 'info');

					}

					$row['bind'] = 'reside';

				} elseif (!empty($row['require']) && empty($_GPC[$row['bind']])) {

					message('请输入'.$row['title'].'！', referer(), 'info');

				}

				$data[$row['bind']] = $_GPC[$row['bind']];

			}

		}

		$check = mc_check($data);

		if(is_error($check)) {

			message($check['message'], referer(), 'error');

		}

		

		$sql = 'SELECT COUNT(*)  FROM ' . tablename('mc_card_members') . " WHERE `uid` = :uid AND `cid` = :cid AND uniacid = :uniacid";

		$count = pdo_fetchcolumn($sql, array(':uid' => $_W['member']['uid'], ':cid' => $_GPC['cardid'], ':uniacid' => $_W['uniacid']));

		if ($count >= 1) {

			message('已领取过该会员卡.', referer(), 'error');

		}



		$record = array(

			'uniacid' => $_W['uniacid'],

			'openid' => $_W['openid'],

			'uid' => $_W['member']['uid'],

			'cid' => $_GPC['cardid'],

			'cardsn' => $data['mobile'],

			'status' => '1',

			'createtime' => TIMESTAMP,

			'endtime' => TIMESTAMP

		);



		if(pdo_insert('mc_card_members', $record)) {

			if(!empty($data)){

				mc_update($_W['member']['uid'], $data);

			}

						$notice = '';

			if(is_array($setting['grant'])) {

				if($setting['grant']['credit1'] > 0) {

					$log = array(

						$_W['member']['uid'],

						"领取会员卡，赠送{$setting['grant']['credit1']}积分"

					);

					mc_credit_update($_W['member']['uid'], 'credit1', $setting['grant']['credit1'], $log);

					$notice .= "赠送【{$setting['grant']['credit1']}】积分";

				}

				if($setting['grant']['credit2'] > 0) {

					$log = array(

						$_W['member']['uid'],

						"领取会员卡，赠送{$setting['credit2']['credit1']}余额"

					);

					mc_credit_update($_W['member']['uid'], 'credit2', $setting['grant']['credit2'], $log);

					$notice .= ",赠送【{$setting['grant']['credit2']}】余额";

				}

				if (!empty($setting['grant']['coupon']) && is_array($setting['grant']['coupon'])) {

					foreach ($setting['grant']['coupon'] as $grant_coupon) {

						$status = activity_coupon_grant($grant_coupon['coupon'], $_W['member']['uid']);

						if(!is_error($status)) {

							$coupon_title .= ",{$grant_coupon['couponTitle']}";

						}

					}

					$notice .= ",赠送【{$coupon_title}】优惠券";

				}

			}

			$time = date('Y-m-d H:i');

			$url = murl('mc/card/mycard/', array(), true, true);

			$title = "【{$_W['account']['name']}】- 领取会员卡通知\n";

			$info = "您在{$time}成功领取会员卡，{$notice}。\n\n";

			$info .= "<a href='{$url}'>点击查看详情</a>";

			$status = mc_notice_custom_text($_W['openid'], $title, $info);

			message("领取会员卡成功", url('mc/card/mycard'), 'success');

		} else {

			message('领取会员卡失败.', referer(), 'error');

		}

	}

}







if ($do == 'mycard') {

	$mcard = pdo_fetch('SELECT * FROM ' . tablename('mc_card_members') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid']));

	$title = $setting['title'];

	if(empty($mcard)) {

		header('Location:' . url('mc/card/receive_card'));

	}

	if(empty($mcard['openid']) && !empty($_W['openid'])) {

		pdo_update('mc_card_members', array('openid' => $_W['openid']), array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']));

	}

	if (!empty($mcard['status'])) {

		$setting = pdo_fetch('SELECT * FROM ' . tablename('mc_card') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));

		if(!empty($setting)) {

			$setting['color'] = iunserializer($setting['color']);

			$setting['background'] = iunserializer($setting['background']);;

		}

	}

	load()->model('activity');

	$coupons = activity_coupon_owned();

	$nums_recharge = iunserializer($setting['nums']);

	$times_recharge = iunserializer($setting['times']);

	$total = count($coupons);

	load()->model('card');

	$notice_count = card_notice_stat();

}





if($do == 'add_recharge') {

	$type = trim($_GPC['type']);

	$mcard = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']));

	$mcard['status'] = '1';

	if (!empty($mcard['status'])) {

		$setting = pdo_fetch('SELECT * FROM ' . tablename('mc_card') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));

		$setting = iunserializer($setting[$type]);

	}

}



if($do == 'recharge_record') {

	$period = $_GPC['period'];

	$period_date = ($period == '1') ? date('Y.m', strtotime('now')) : date('Y.m', strtotime('now' . ($_GPC['period'] * 1) . ' month'));

	$starttime = ($period == '1') ? date('Ym01') : date('Ym01', strtotime(1*$period . "month"));

	$endtime = date('Ymd', strtotime("$starttime + 1 month - 1 day"));

	$setting = pdo_get('mc_card', array('uniacid' => $_W['uniacid']), array('nums_text', 'times_text'));

	$card = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']));

	$type = trim($_GPC['type']);

	$where = ' WHERE uniacid = :uniacid AND uid = :uid AND type = :type AND addtime >= :starttime AND addtime <= :endtime';

	$params = array(

		':uniacid' => $_W['uniacid'],

		':uid' => $_W['member']['uid'],

		':type' => $type,

		':starttime' => strtotime($starttime),

		':endtime' => strtotime($endtime)

	);

	$data = pdo_fetchall('SELECT * FROM ' . tablename('mc_card_record') . $where . ' ORDER BY id DESC ', $params);

}



if ($do == 'personal_info') {

	$setting = pdo_get('mc_card', array('uniacid' => $_W['uniacid'], 'status' => '1'));

	if (!empty($setting['fields'])) {

		$mc_fields = iunserializer($setting['fields']);

		foreach ($mc_fields as $key => &$row) {

			if (!empty($row['require']) && ($row['bind'] == 'birth' || $row['bind'] == 'birthyear') || $row['bind'] == 'birthday') {

				$row['bind'] = 'birth';

			} elseif (!empty($row['require']) && ($row['bind'] == 'resideprovince' || $row['bind'] == 'residecity' || $row['bind'] == 'residedist')) {

				$row['bind'] = 'reside';

			}

			if ($row['bind'] == 'mobile') {

				unset($mc_fields[$key]);

			}



		}

	}

	$profile = mc_fetch($_W['member']['uid']);

	$mcard = pdo_fetch('SELECT * FROM ' . tablename('mc_card_members') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $_W['member']['uid']));



}



if ($do == 'consume') {

	$card_settings = card_setting();

	$discount_params = $card_settings['discount'];

	$group = mc_fetch($_W['member']['uid'], array('groupid'));

	if(checksubmit()) {

		$fee = trim($_GPC['fee']);

		$body = trim($_GPC['body']) ? trim($_GPC['body']) : '收银台收款' . $fee . '元';

		if (empty($fee) || $fee  <= 0) {

			message('收款金额有误', '', 'error');

		}

		$final_fee = card_discount_fee($fee);

		$data = array(

			'uniacid' => $_W['uniacid'],

			'clerk_id' => 0,

			'clerk_type' => 0,

			'store_id' => 0,

			'body' => $body,

			'fee' => $fee,

			'final_fee' => $final_fee,

			'credit_status' => 1,

			'createtime' => TIMESTAMP,

		);

		pdo_insert('paycenter_order', $data);

		$id = pdo_insertid();

		header('Location:' . murl('entry', array('m' => 'paycenter', 'do' => 'pay', 'id' => $id), true, true));

		die;

	}

}



template('mc/card');